
package riccsson.system.test;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.view.Gravity;
import android.view.View.OnKeyListener;
import android.view.KeyEvent;

import java.util.ArrayList;

import riccsson.system.test.ConsoleView;

public class main extends Activity
{
	private static native void StartNative();
	private static native void InputKey(int key);

	private ConsoleView scrollView;
	Thread thread;

    //-----------------------------------------------------------
    @Override public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
		ScreenView console = new ScreenView(this);
		console.setGravity(Gravity.TOP);
        setContentView(console);
		
		// Set text checker.
		//editText.addTextChangedListener(new TextValidator(editText)
		//{
		//	@Override public void validate(TextView textView, String text)
		//	{
		//	   
		//	}
		//});
		
		console.setOnKeyListener(new OnKeyListener()
		{
			public boolean onKey(View v, int keyCode, KeyEvent event)
			{
				// Input to native code.
				InputKey(keyCode);
				
				return false;
			}
		});
		
		thread = new Thread(new Runnable()
		{
			@Override public void run()
			{
				StartNative();
			}
		});
		thread.start();
    }
	
    //-----------------------------------------------------------
	@Override public void onPause()
	{
		ScreenView.stop();
		
		super.onPause();
	}

    //-----------------------------------------------------------
    static
	{
        System.loadLibrary("Riccsson.System.Test_android");
    }
}

class ScreenView extends EditText
{
	private static EditText s_instance = null;
	private static ArrayList<String> s_queueMessages = new ArrayList<String>();
	private static boolean s_running;
	
    private EditText scrollView;
	
	private static final int DISPLAY_DATA = 1;
    // this handler will receive a delayed message
    private Handler mHandler = new Handler()
	{
        @Override public void handleMessage(Message msg)
		{
            // Do task here
            if (msg.what == DISPLAY_DATA)
			{
				synchronized(s_instance)
				{
					for(int i = 0; i < s_queueMessages.size(); i++)
					{
						// Update text to the console.
						s_instance.append(s_queueMessages.get(i));
					}
					s_queueMessages.clear();
					
					//s_instance.append(".");
					
					if(s_running)
					{
						mHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 100);
					}
				}
			}
        }
	};
	
	//-----------------------------------------------------------
	public static void stop()
	{
		s_running = false;
	}
	
    //-----------------------------------------------------------
	public static void WriteLine(String message)
	{
		synchronized(s_instance)
		{
			s_queueMessages.add(message);
		}
	}

    //-----------------------------------------------------------
    public ScreenView(Context context)
	{
        super(context);
		s_instance = this;
		
		s_running = true;
		mHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 100);
    }
}

//public abstract class TextValidator implements TextWatcher
// {
//    private TextView textView;
//
//    public TextValidator(TextView textView)
//	{
//        this.textView = textView;
//    }
//
//    @Override final public void afterTextChanged(Editable s)
//	{
//        //String text = textView.getText().toString();
//        //validate(textView, text);
//    }
//
//    @Override final public void beforeTextChanged(CharSequence s, int start, int count, int after) { /* Don't care */ }
//
//    @Override final public void onTextChanged(CharSequence s, int start, int before, int count)
//	{
//		s.toString();
//	}
//}